using Gee;
using Gtk;
using Meridian.Data;

namespace Meridian.Side {

public abstract class PageItem : SideItem, GLib.Object {

    public string title {get; set construct;}
    public string url {get; set construct;}
    public int visits {get; private set; default = 0;}
    public DateTime added {get; construct;}
    public DateTime last_visited {get; private set;}

    public PageItem (string title, string url) {
        GLib.Object(title : title, url : url, added : new DateTime.now_local ());
        this.last_visited = new DateTime.now_local ();
    }
    
    public PageItem.recover (string title, string url, int visits, DateTime added, DateTime last_visited) {
        GLib.Object(title : title, url : url, visits : visits, added : added);
        this.last_visited = last_visited;
    }
    
    public abstract DataTable get_table ();
    
    public HashMap<string, string> get_fields () {
        var fields = new HashMap<string, string> ();
        fields.set ("title", this.title);
        fields.set ("url", this.url);
        fields.set ("visits", this.visits.to_string ());
        fields.set ("added", this.added.format (Dao.DATE_FORMAT));
        fields.set ("last_visited", this.last_visited.format (Dao.DATE_FORMAT));
        return fields;
    }

    public void visit () {
        this.visits ++;
        this.last_visited = new DateTime.now_local ();
    }

}
}